package com.learnyou.server.xingzuo.common.util;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.MDC;

import java.util.UUID;

public class LogDiagnosticUtil {

    /**
     * 请求链路id
     */
    public static final String REQUEST_ID = "requestId";

    /**
     * 初始化requestId
     */
    public static void initRequestId() {
        if (StringUtils.isBlank(MDC.get(REQUEST_ID))) {
            MDC.put(REQUEST_ID, UUID.randomUUID().toString().replace("-", ""));
        }
    }

    /**
     * 存值
     *
     * @param key
     * @param value
     */
    public static void put(String key, String value) {
        if (StringUtils.isNotBlank(key) && StringUtils.isNotBlank(value)) {
            MDC.put(key, value);
        }
    }




    /**
     * 删除该请求线程的值
     */
    public static void clear() {
        MDC.clear();
    }

    /**
     * 获取值
     *
     * @param key
     * @return
     */
    public static String get(String key) {
        if (StringUtils.isNotBlank(key)) {
            return MDC.get(key);
        }
        return null;
    }

}
